{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Hello World"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "print(\"Hello World!\")\n",
    "print(\"o----\")\n",
    "print(\" ||||\")"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Hello World!\n",
      "o----\n",
      " ||||\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "birth_year = input(\"Birth year: \")\n",
    "age = 2021 - eval(birth_year)\n",
    "print(age)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "21\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "name = \"Lucca\"\n",
    "print(name[1:-1])\n",
    "# 包括 1 到 -2 的所有字符"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "ucc\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "# 格式化\n",
    "first = \"John\"\n",
    "last = \"Smith\"\n",
    "# the formatted string ↓\n",
    "message = f\"{first} [{last}] is a coder\"\n",
    "print(message)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "John [Smith] is a coder\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# String Methods"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "# len() : function\n",
    "# str.upper() : method\n",
    "course = \"Python for Beginners\"\n",
    "print(len(course))\n",
    "print(course.upper())\n",
    "print(course.find('n'))     # 5\n",
    "print(course.rfind('n'))    # 16\n",
    "print(course.replace('Beginners', \"Also Beginners\"))\n",
    "print(\"Python\" in course)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "20\n",
      "PYTHON FOR BEGINNERS\n",
      "5\n",
      "16\n",
      "Python for Also Beginners\n",
      "True\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Exceptions"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "try:\n",
    "    age = int(input(\"Age: \"))\n",
    "    income = 20000\n",
    "    risk = income / age\n",
    "    print(age)\n",
    "except ZeroDivisionError:\n",
    "    print(\"Age cannot be 0.\")\n",
    "except ValueError:\n",
    "    print(\"Invalid value.\")"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Invalid value.\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Comments\n",
    "Tell others what you wanna do"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Classes"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "# use EmailClient instead of email_client when creating a type\n",
    "class Point:\n",
    "    def move(self):\n",
    "        print(\"move\")\n",
    "\n",
    "    def draw(self):\n",
    "        print(\"draw\")"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "# methods\n",
    "point1 = Point()\n",
    "point1.draw()\n",
    "point1.move()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "draw\n",
      "move\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "source": [
    "# attributes\n",
    "point1.x = 10\n",
    "point1.y = 20\n",
    "print(point1.x)\n",
    "print(point1.y)\n",
    "print(point1.z)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "10\n",
      "20\n"
     ]
    },
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'Point' object has no attribute 'z'",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-9-1f25503c34d4>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m      4\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mpoint1\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mx\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m      5\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mpoint1\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0my\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m----> 6\u001B[1;33m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mpoint1\u001B[0m\u001B[1;33m.\u001B[0m\u001B[0mz\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[1;31mAttributeError\u001B[0m: 'Point' object has no attribute 'z'"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Constructors\n",
    "构造函数 `__init__`"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "source": [
    "class Point:\n",
    "    # --- Here to start --- #\n",
    "    def __init__(self, x, y):\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "    # ---- Here to end ---- #\n",
    "    \n",
    "    def move(self):\n",
    "        print(\"move\")\n",
    "\n",
    "    def draw(self):\n",
    "        print(\"draw\")"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "source": [
    "point = Point(10, 20)\n",
    "print(point.x)\n",
    "print(point.y)\n",
    "point.x = 11\n",
    "print(point.x)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "10\n",
      "20\n",
      "11\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Exercise\n",
    "define a new type called \"person\"\n",
    "each \"person\" obj has an attribute \"name\"\n",
    "and a \"talk()\" method"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "source": [
    "class Person:\n",
    "    def __init__(self, name):\n",
    "        self.name = name\n",
    "\n",
    "    def talk(self):\n",
    "        print(f\"Hi, I am {self.name}\")"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "source": [
    "john = Person(\"John Smith\")\n",
    "print(john.name)\n",
    "john.talk()\n",
    "\n",
    "bob = Person(\"Bob Smith\")\n",
    "bob.talk()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "John Smith\n",
      "Hi, I am John Smith\n",
      "Hi, I am Bob Smith\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Inheritance\n",
    "继承"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "class Mammal:\n",
    "    def walk(self):\n",
    "        print(\"walk\")\n",
    "\n",
    "class Dog(Mammal):\n",
    "    def bark(self):\n",
    "        print(\"bark\")\n",
    "\n",
    "class Cat(Mammal):\n",
    "    pass"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "dog1 = Dog()\n",
    "dog1.walk()\n",
    "dog1.bark()"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "walk\n",
      "bark\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Modules\n",
    "模块，见 main.py 和 converters.py"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Packages\n",
    "包，把多个模块打包到一个目录或文件夹\n",
    "\n",
    "*在目录下必须有一个 `__init__.py`*\n",
    "\n",
    "见 main.py 中和 ecommerce 目录"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Generating Random Values\n",
    "*[python 3 module index](https://docs.python.org/3/py-modindex.html)*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "source": [
    "import random\n",
    "print(random.random())\n",
    "# [1, 100]\n",
    "print(random.randint(1, 100))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0.8305991868829514\n",
      "72\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "source": [
    "class Dice:\n",
    "    def roll(self):\n",
    "        first = random.randint(1, 6)\n",
    "        second = random.randint(1, 6)\n",
    "        return first, second\n",
    "\n",
    "d = Dice()\n",
    "print(d.roll())"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "(2, 2)\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Files and Directories\n",
    "*[pathlib](https://docs.python.org/3/library/pathlib.html#module-pathlib)*"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "source": [
    "from pathlib import Path\n",
    "# Absolute path\n",
    "# Relative path"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "source": [
    "path = Path()\n",
    "print(path)\n",
    "\n",
    "path1 = Path(\"ecommerce\")\n",
    "print(path1)\n",
    "print(path1.exists())\n",
    "\n",
    "path2 = Path(\"ecommerce1\")\n",
    "print(path2)\n",
    "print(path2.exists())\n",
    "# 创建目录\n",
    "path2.mkdir()\n",
    "# 删除目录\n",
    "path2.rmdir()\n",
    "\n",
    "# 搜索目录，返回生成器\n",
    "print(path.glob(\"*.py\"))\n",
    "for file in path.glob(\"*.py\"):\n",
    "    print(file)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      ".\n",
      "ecommerce\n",
      "True\n",
      "ecommerce1\n",
      "False\n",
      "<generator object Path.glob at 0x0000027C58F8C4C8>\n",
      "converters.py\n",
      "main.py\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "# Pypi and Pip (Huh?)\n",
    "*[Pypi](https://pypi.org/)*\n",
    "\n",
    "安装各种各样的包\n",
    "\n",
    "`pip install {name}`"
   ],
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.7.4",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "language": "python",
   "display_name": "Python 3 (ipykernel)"
  },
  "interpreter": {
   "hash": "a8f61be024eba58adef938c9aa1e29e02cb3dece83a5348b1a2dafd16a070453"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}